package com.jims.master.modules.orders.vo;

import com.jims.master.modules.orders.entity.Orders;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;

/**
 * Created by 陈晓阳 on 2017/7/15.
 */
public class CountTask extends RecursiveTask<List<CountDrug>>{

    public static final ForkJoinPool forkJoinPool=new ForkJoinPool();
    private   int size=0;
    private static int THRESHOLD=1;
    private  int days=0;
    private static CountTask countTask=null;
    public static String COLUMN="create_date";
    public static Integer MM=6;
    private  List<CountDrug> countDrugList=new ArrayList<CountDrug>();
    private  List<Orders> dispOrderDetails=new ArrayList<Orders>();

    public static CountTask getInstance(int size,int days,List<CountDrug> countDrugList,List<Orders> dispOrderDetails ){
        if(countTask==null){
            return new CountTask(size,days,countDrugList,dispOrderDetails);
        }

        return countTask;
    }
    public CountTask(int size,int days,List<CountDrug> countDrugList,List<Orders> dispOrderDetails){
        this.size=size;
        this.days=days;
        this.countDrugList=countDrugList;
        this.dispOrderDetails=dispOrderDetails;
    }
    @Override
    protected List<CountDrug> compute() {
        List<CountDrug> returnVal=new ArrayList<CountDrug>();
        boolean canCompute=size>THRESHOLD;
        if(!canCompute){
            returnVal=singleTask(countDrugList.get(0));
        }else{
            for (CountDrug countDrug : countDrugList) {
                    countTask=getInstance(1,days,countDrugList,dispOrderDetails);
                    Future<List<CountDrug>> r=countTask.fork();
            }
        }
        return returnVal;
    }

    private double countAmount(String clinicItemId, List<Orders> dispOrderDetails) {
        double amount = 0;
        for (Orders order : dispOrderDetails) {
            String cId = order.getClinicItemId();
            if (clinicItemId.equals(cId)) {
                amount += order.getAmount();
            }
        }
        return amount;
    }

    /**
     * 单个任务的执行
     * @param countDrug
     * @return
     */
    public List<CountDrug> singleTask(CountDrug countDrug){
        List<CountDrug> returnVal=new ArrayList<CountDrug>();
        double amount = this.countAmount(countDrug.getClinicItemId(), dispOrderDetails);
        if (amount <= 0) {
            return null;
        }
        countDrug.setAmount(amount);
        countDrug.setDays(days);
        returnVal.add(countDrug);
        return returnVal;
    }
    public final Integer lastMath=18;
}
